package com.js.nickcar.module.addcar.uploadphoto;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.js.nickcar.R;
import com.js.nickcar.base.BaseHolder;
import com.js.nickcar.entity.local.UploadPhotoBean;

import java.util.ArrayList;

/**
 * Created by LZY on 2017/10/7.
 */
public class CarPhotoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public final Context mContext;
    public final LayoutInflater mInflater;

    private ArrayList<UploadPhotoBean> mItems = new ArrayList<>();

    public static final int VIEW_TYPE_NOR = 0;
    public static final int VIEW_TYPE_ADD = 1;
    public int photoWidth = -1;
    public String mType;


    public CarPhotoAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_NOR:
                return new CarPhotoHolder(mInflater.inflate(R.layout.item_car_photo, parent, false));
            default:
                return new AddPhotoHolder(mInflater.inflate(R.layout.item_add_car_photo, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_NOR:
                onBindCarPhotoHolder((CarPhotoHolder) holder, getItem(position), position);
                break;
            default:
                onBindAddPhotoHolder((AddPhotoHolder) holder, position);
                break;
        }
    }

    private void onBindCarPhotoHolder(CarPhotoHolder holder, final UploadPhotoBean item, final int position) {
        Glide.with(mContext)
                .load(TextUtils.isEmpty(item.getRemotePath()) ? item.getRemotePath() : item.getLocalPath())
                .centerCrop()
                .into(holder.ivCarPhoto);
        holder.ivRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItems.remove(item);
                notifyItemRangeRemoved(position, 1);
                onCarPhotoListener.remove(mType, item, position);
            }
        });
    }

    private void onBindAddPhotoHolder(AddPhotoHolder holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCarPhotoListener != null) {
                    UploadPhotoBean uploadPhotoBean = new UploadPhotoBean();
                    uploadPhotoBean.setType(mType);
                    onCarPhotoListener.onAddPhoto(uploadPhotoBean);
                }
            }
        });
    }

    public UploadPhotoBean getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public int getItemCount() {
        return mItems.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == mItems.size()) {
            return VIEW_TYPE_ADD;
        } else {
            return VIEW_TYPE_NOR;
        }
    }

    public void setData(ArrayList<UploadPhotoBean> uploadPhotos, String type) {
        mType = type;
        mItems.clear();
        mItems.addAll(uploadPhotos);
        notifyDataSetChanged();
    }

    public static class AddPhotoHolder extends BaseHolder {

        public final ImageView ivAddPhoto;

        public AddPhotoHolder(View itemView) {
            super(itemView);
            ivAddPhoto = findViewById(R.id.iv_add_photo);
        }
    }

    public static class CarPhotoHolder extends BaseHolder {

        public final ImageView ivCarPhoto;
        public final ImageView ivRemove;

        public CarPhotoHolder(View itemView) {
            super(itemView);
            ivCarPhoto = findViewById(R.id.iv_car_photo);
            ivRemove = findViewById(R.id.iv_remove);
        }
    }

    private OnCarPhotoListener onCarPhotoListener;

    public void setOnCarPhotoListener(OnCarPhotoListener onCarPhotoListener) {
        this.onCarPhotoListener = onCarPhotoListener;
    }

    public interface OnCarPhotoListener {
        void onAddPhoto(UploadPhotoBean uploadPhotoBean);

        void remove(String type, UploadPhotoBean item, int position);
    }

}
